﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.QRUPC;
using MangoMes.Commont.Domain.SellStocks;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.QRUPCWrite
{
    public class QUCodeUpdateHandler : IRequestHandler<QUCodeUpdateCommand, ResultsDTO>
    {
        private readonly IBateRepository<QUCode> _qucodeRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="qucodeRepository"></param>
        public QUCodeUpdateHandler(IBateRepository<QUCode> qucodeRepository)
        {
            _qucodeRepository = qucodeRepository;
        }
        /// <summary>
        /// 修改赋码管理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO> Handle(QUCodeUpdateCommand request, CancellationToken cancellationToken)
        {
            var list = new ResultsDTO() { Code = 0 };

            //连接数据库
            DapperHelper dapper = new DapperHelper();
            var conn = dapper.MangoMesWrite;

            //实体
            var qucode = new QUCode();
            var str = "update qucode set QUCodeFormat='" + request.QUCodeFormat + "',QUCodeType='" + request.QUCodeType + "',ProductManageName='" + request.ProductManageName + "',ProductManageNode='" + request.ProductManageNode + "',QUCodeStart='" + request.QUCodeStart + "'," +
                "QUCodeMatter='" + request.QUCodeMatter + "',QUCodeRemark='" + request.QUCodeRemark + "',BarCode='" + request.BarCode + "' where QUCodeId='" + request.QUCodeId + "'";
            var result = _qucodeRepository.Add(str, qucode, conn);
            if (result != null)
            {
                list.Code = 1;
                list.Message = "修改成功";
            }
            return list;
        }
    }
}
